import util

class Player:
    def __init__(self,name,job):
        self.name = name
        self.job = job
        self.max_life = 100
        self.life = self.max_life
        self.exp = 0
        self.level = 1
        self.attack = 0
        self.defense = 0
        self.agile = 0
        self.crit_rate = 0
        self.crit_damage = 0
        self.isLife = True
        util.set_player(self)

    def show(self):
        print("*"*50)
        print(f"{self.name}\t职业:{self.job}")
        print(f"等级:{self.level}\t经验值:{self.exp}\t当前生命:{self.life}")
        print(f"攻击力:{self.attack}\t防御力:{self.defense}\t敏捷:{self.agile}\t暴击率:{self.crit_rate}\t  暴击伤害:{self.crit_damage}")
        print("*" * 50)

    def fight(self,enemy):
        while self.isLife and enemy.isLife:
            print("="*50)
            #1、输出打的口号
            print(f"{self.name}举起{self.weapon}砍向{enemy.name}:哇哈哈哈！接招!")
            damage = self.attack - enemy.defense
            if damage < 0: damage = 1
            ##添加暴击
            if util.check_rate(self.crit_rate):
                print(f"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{self.name}：哇哈哈哈哈，天神赐予我力量!看招吧!")
                damage+=damage*(self.crit_damage/100)
            enemy.injured(self,damage)
            #2、添加经验
            if not enemy.isLife:
                self.exp += enemy.get_exp
            #3、判断是否升级
            if self.exp >= util.get_upgrade_exp(self.level):
                self.upgrade()

    def upgrade(self):
        print(f"{self.name}：当当当！升级啦！")
        self.level += 1
        self.max_life +=10
        self.life = self.max_life
        self.attack+=5
        self.agile+=2
        self.defense+=2
        self.crit_rate+=1
        self.crit_damage+=50

    def injured(self,damage):
        if util.check_rate(self.agile):
            print(f"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4{self.name}:嘻嘻嘻嘻!打不到我哟!")
            return
        print(f"{self.name}:哇哇!居然被打了!")
        self.life -= damage
        if self.life<=0:
            self.isLife = False
            print(f"{self.name}:霸业未成!天妒英才!")
            print("大侠重新来过!")

class Enemy:
    def __init__(self,name,type):
        self.type = type
        if self.type == 'boss':
            self.name = name
        else:
            self.name = name+"的"+type
        self.life = 0
        self.exp = 0
        self.attack = 0
        self.defense = 0
        self.agile = 0
        self.crit_rate = 0
        self.crit_damage = 0
        self.isLife = True
        util.set_enemy(self)

    def injured(self,player,lost_life):
        if lost_life==1:
            print(f"{self.name}:呵呵呵!打不动我")
        else:
            print(f"{self.name}:饿啊!鼠辈，竟然敢伤我!")
        #1、丢失生命
        self.life -= lost_life
        #2、判断是否死亡
        if self.life<=0:
            self.isLife = False
            print(f"{self.name}:哇啊啊啊啊啊啊！")
        else:
            print(f"{self.name}:居然敢打我，看招!")
            damage = self.attack - player.defense
            if damage <=0: damage = 1
            player.injured(damage)

    def show(self):
        print("*"*50)
        print(f"{self.name}\t类型:{self.type}")
        print(f"当前生命:{self.life}\t攻击力:{self.attack}\t防御力:{self.defense}\t敏捷:{self.agile}\t暴击率:{self.crit_rate}\t  暴击伤害:{self.crit_damage}")
        print("*" * 50)

if __name__ == '__main__':
    knight = Player("李寻欢","刺客")
    knight.show()
    e1 = Enemy("弱小","僵尸")
    e2 = Enemy("凶恶","吸血鬼")
    e3 = Enemy("强壮","骷髅")
    e4 = Enemy("弱小","骷髅")
    e6 = Enemy("凶恶", "吸血鬼")
    e7 = Enemy("凶恶", "吸血鬼")
    e5 = Enemy("埃尔特六世","boss")
    e1.show()
    knight.fight(e1)
    knight.fight(e2)
    knight.fight(e3)
    knight.fight(e4)
    knight.fight(e6)
    knight.fight(e7)
    knight.fight(e5)
    knight.show()
    e5.show()